package fuzion24.application.vulnerability.detector;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.framework.util.jar.JarFileHelper;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Pair;


import java.util.ArrayList;
import java.util.List;

public class ApplicationVulnerabilityTester extends AsyncTask<Void,Integer,List<ApplicationVulnTestResult>> {
        private final Context mCtx;
        private final ApplicationResultsCallback mCustomCallback;
        private final Boolean mShowWaitDiaglog;

        private ProgressDialog mProgressDialog;

        public ApplicationVulnerabilityTester(Context actv, Boolean showScanDialog, ApplicationResultsCallback callback){
            mCtx = actv;
            mShowWaitDiaglog = showScanDialog;
            mCustomCallback = callback;
        }

        @Override
        protected void onPreExecute()
        {
            if(mShowWaitDiaglog)
                mProgressDialog = ProgressDialog.show(mCtx, "Application Vulnerability Checker", "Checking apps for various vulnerabilities", false);
        }

        protected void onProgressUpdate(String... progress) {
            if(mShowWaitDiaglog) {
                Log.d("ANDRO_ASYNC", progress[0]);
                mProgressDialog.setProgress(Integer.parseInt(progress[0]));
            }
        }

        @Override
        protected List<ApplicationVulnTestResult> doInBackground(Void... params)
        {
            PackageManager pm = mCtx.getPackageManager();
            List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_SHARED_LIBRARY_FILES | PackageManager.GET_META_DATA);

            List<ApplicationVulnTestResult> applicationVulnerabilityTestResults = new ArrayList<>();
            List<Pair<ApplicationInfo,Exception>> untestedApps = new ArrayList<>();

            for(int i = 0; i < apps.size(); i++) {

                ApplicationInfo ai = apps.get(i);

                String sourceApk = ai.publicSourceDir;

                boolean apkExploitedBug13678484 = false;

                try {
                    apkExploitedBug13678484 = JarFileHelper.isExploitingBug13678484(sourceApk);
                }catch(Exception e){
                    untestedApps.add(Pair.create(ai,e));
                }

                if(apkExploitedBug13678484){
                    applicationVulnerabilityTestResults.add(ApplicationVulnTestResult.build(ai, null));
                }

            }

            return applicationVulnerabilityTestResults;
        }

        @Override
        protected void onPostExecute(List<ApplicationVulnTestResult> appVulnerabilityResults)
        {
            super.onPostExecute(appVulnerabilityResults);
            if(mCustomCallback != null)
                mCustomCallback.finished(appVulnerabilityResults);

            if(mShowWaitDiaglog)
              mProgressDialog.dismiss();
        }
}